package edu.ptu.acl.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.ptu.acl.common.MyConstants;
import edu.ptu.acl.common.ResultVO;
import edu.ptu.acl.dto.AclTreeDto;
import edu.ptu.acl.dto.param.AclParam;
import edu.ptu.acl.dto.param.IdListParam;
import edu.ptu.acl.entity.Acl;
import edu.ptu.acl.entity.RoleAcl;
import edu.ptu.acl.entity.User;
import edu.ptu.acl.service.IAclService;
import edu.ptu.acl.service.IRoleAclService;
import edu.ptu.acl.utils.SnowIdWorker;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.Min;
import javax.validation.constraints.Pattern;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LinJC
 * @since 2022-03-15
 */
@RestController
@RequestMapping("/acl")
@Validated
public class AclController {

    @Resource
    private IAclService aclService;
    @Resource
    private IRoleAclService roleAclService;

    //权限树形结构获取
    @GetMapping("/getAclTreeList/{parentId}")
    public ResultVO getAclListByAclModuleId(@PathVariable Long parentId){
        QueryWrapper<Acl> aclQueryWrapper = new QueryWrapper<>();
        aclQueryWrapper.eq("parent_id",parentId);
        List<Acl> list = aclService.list(aclQueryWrapper);
        int count;List<AclTreeDto> aclTreeDtoList = new ArrayList<>();
        for (Acl acl : list) {
            aclQueryWrapper.clear();
            aclQueryWrapper.eq("parent_id",acl.getId());
            count = aclService.count(aclQueryWrapper);
            AclTreeDto aclTreeDto = AclTreeDto.copyDept(acl);
            if (count >=1 ){
                aclTreeDto.setHasChildren(true);
            }
            aclTreeDtoList.add(aclTreeDto);
        }
        return ResultVO.success(aclTreeDtoList);
    }

    //权限树形结构获取
    @GetMapping("/getAllAclTreeList")
    public ResultVO getAclListByAclModuleId(){
        List<AclTreeDto> aclTreeDto = aclService.getAclTreeDto();
        return ResultVO.success(aclTreeDto);
    }

    //新增权限
    @PostMapping("/saveAcl")
    public ResultVO saveAcl(@RequestBody @Validated(AclParam.Save.class) AclParam aclParam){
        Acl acl = Acl.builder().name(aclParam.getName()).status(aclParam.getStatus())
                .seq(aclParam.getSeq()).parentId(aclParam.getParentId())
                .remark(aclParam.getRemark()).url(aclParam.getUrl()).buttonId(aclParam.getButtonId())
                .component(aclParam.getComponent()).type(aclParam.getType()).build();
        aclService.save(acl);
        return ResultVO.success();
    }
    //编辑权限
    @PutMapping("/updateAcl")
    public ResultVO updateAcl(@RequestBody @Validated(AclParam.Update.class) AclParam aclParam){
        Acl acl = Acl.builder().id(aclParam.getId()).name(aclParam.getName()).status(aclParam.getStatus())
                .seq(aclParam.getSeq()).version(aclParam.getVersion()).parentId(aclParam.getParentId())
                .remark(aclParam.getRemark()).url(aclParam.getUrl()).buttonId(aclParam.getButtonId())
                .component(aclParam.getComponent()).type(aclParam.getType()).build();
        if (aclService.updateById(acl)) {
            return ResultVO.success();
        }
        return ResultVO.fail(MyConstants.ResultCode.OPTIMISTIC_LOCKING);
    }
    //	多选冻结
    // todo :魔法值：正则规则。后续抽取
    @PutMapping("/freezeAcl/{status}")
    public ResultVO updateStatus(@RequestBody @Validated IdListParam idListParam
            ,@PathVariable @Pattern(regexp = "[10]",message = "状态异常") String status){
        UpdateWrapper updateWrapper = new UpdateWrapper();
        for (Long id : idListParam.getIds()) {
            updateWrapper.clear();
            updateWrapper.set("status",status);
            updateWrapper.eq("id",id);
            aclService.update(updateWrapper);
        }
        return ResultVO.success();
    }
    //	多选删除
    @DeleteMapping("/deleteAcl")
    public ResultVO deleteAcl(@RequestBody @Validated IdListParam idListParam){


        aclService.deleteAcl(Arrays.asList(idListParam.getIds()));


        return ResultVO.success();
    }
    //	删除
    @DeleteMapping("/deleteOneAcl/{id}")
    public ResultVO deleteAcl(@PathVariable Long id){
        ArrayList<Long> ids = new ArrayList<>();
        ids.add(id);
        aclService.deleteAcl(ids);
        return ResultVO.success();
    }

}
